var prefix = jsctx + "/memberCard";

var memberCard = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listMemberCard",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    cache: false,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            operateStatus: $("#operateStatus").val().trim(),
                            nickname: $("#nickname").val().trim(),
                            mobile: $("#mobile").val().trim(),
                            cardNo: $("#cardNo").val().trim()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '',width:'50',align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },

                        {field: 'cardNo', title: '充电卡号', width:'100',align: 'left'},
                        {
                            field: 'operateStatus', title: '会员绑定状态', width:'100',align: 'center',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span class="label label-warning">未绑定</span>'
                                } else if (value == '1') {
                                    return '<span class="label label-info">已绑定</span>'
                                } else if (value == '2') {
                                    return '<span class="label label-danger">已注销</span>'
                                }
                            }
                        },
                        {field: 'nickname', title: '会员名称', width:'100',align: 'left'},
                        {field: 'mobile', title: '会员手机号', width:'100',align: 'left'},
                        {field: 'changeTime', title: '绑定时间', width:'100',align: 'left'},
                        {field: 'createdTime', title: '创建时间', width:'100',align: 'left'},
                        {
                            title: '操作',
                            field: 'roleId',
                            width:'200',align: 'center',
                            formatter: function (value, row, index) {

                                if (row.operateStatus == '0') {
                                    var a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="绑定" onclick="memberCard.bindAndChange(\''
                                        + row.cardId
                                        + '\')">绑定</a> ';
                                } else if (row.operateStatus == '1') {
                                    var a = '<a class="btn btn-warning btn-sm ' + has_edit + '" href="#" title="换绑" onclick="memberCard.bindAndChange(\''
                                        + row.cardId
                                        + '\')">换绑</a> ';
                                }

                                var b = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="编辑" onclick="memberCard.editMemberCard(\''
                                    + row.cardId
                                    + '\')">编辑</a> ';

                                if (row.operateStatus == '2') {
                                    var c = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="已注销" onclick="">已注销</a> ';
                                } else if (row.operateStatus != '2') {
                                    var c = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="注销" onclick="memberCard.cancelCard(\''
                                        + row.cardId
                                        + '\')">注销</a> ';
                                }

                                var d = '<a class="btn btn-success btn-sm ' + has_edit + '" href="#" title="历史" onclick="memberCard.memberCardRecord(\''
                                    + row.cardId
                                    + '\')">历史</a> ';

                                if (row.operateStatus == '2') {
                                    return c + d;
                                } else {
                                    return a + b + c + d;
                                }
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function goBack() {
        location.href = prefix + "/index";
    }

    function bindAndChange(cardId) {
        location.href = prefix + "/bindAndChange/" + cardId;
    }

    function addMemberCard() {
        location.href = prefix + "/addMemberCard";
    }

    function editMemberCard(cardId) {
        location.href = prefix + "/editMemberCard/" + cardId;
    }

    function memberCardRecord(cardId) {
        location.href = prefix + "/memberCardRecord/" + cardId;
    }
    
    function cancelCard(cardId) {
        layer.confirm('是否确定注销该充电卡?注销后,会员将不能使用此卡进行充电操作,同事该会员与卡关系解绑.', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/cancelCard",
                type: "post",
                data: {
                    'cardId': cardId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("注销成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }


    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        memberCardRecord: memberCardRecord,
        addMemberCard: addMemberCard,
        bindAndChange: bindAndChange,
        editMemberCard: editMemberCard,
        cancelCard:cancelCard
    };
})();


var addMemberCard = (function () {
    function generateCardNumber() {
        $.ajax({
            cache: true,
            type: "get",
            url: prefix + "/generateCardNumber",
            data: {
                number: $("#number").val()
            },
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    $("#cardNo").text(data.data)
                    parent.layer.msg("操作成功");
                } else {
                    parent.layer.alert(data.message)
                }

            }
        });
    }


    function init() {
        validateRule();
    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    function save() {
        $("#roleIds").val(getCheckedRoles());
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/saveCardNumber",
            data: $('#frmMemberCard').serialize(),
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    parent.layer.msg("操作成功");
                    window.history.back();
                } else {
                    parent.layer.alert(data.message)
                }

            }
        });
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmMemberCard").validate({
            rules: {},
            messages: {},
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init,
        generateCardNumber: generateCardNumber
    };
})();


var bindAndChange = (function () {
    function init() {
        validateRule();
    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    function save() {
        $("#roleIds").val(getCheckedRoles());
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/bindMobile",
            data: $('#frmBindAndChange').serialize(),
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    parent.layer.msg("操作成功");
                    window.history.back();
                } else {
                    parent.layer.alert(data.message)
                }

            }
        });
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmBindAndChange").validate({
            rules: {},
            messages: {},
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();


var editMemberCard = (function () {

    function init() {
        validateRule();
    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    function save() {
        $("#roleIds").val(getCheckedRoles());
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/saveEditCardNumber",
            data: $('#frmMemberCard').serialize(),
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    parent.layer.msg("操作成功");
                    window.history.back();
                } else {
                    parent.layer.alert(data.message)
                }

            }
        });
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmMemberCard").validate({
            rules: {},
            messages: {},
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();

